package com.hkbigdata.StreamCode;

import com.hkbigdata.entry.UserBehavior;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashSet;

/**
 * @author liuanbo
 * @creat 2023-04-26-19:29
 * @see 2194550857@qq.com
 */
public class Flink03_UV_Process {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<UserBehavior> userBehaviorSingleOutputStreamOperator = env.readTextFile("input/UserBehavior.csv")
                .flatMap(new FlatMapFunction<String, UserBehavior>() {
                    @Override
                    public void flatMap(String value, Collector<UserBehavior> out) throws Exception {
                        String[] split = value.split(",");

                        //1.转换为javabean
                        UserBehavior userBehavior = new UserBehavior(
                                Long.parseLong(split[0]),
                                Long.parseLong(split[1]),
                                Integer.parseInt(split[2]),
                                split[3],
                                Long.parseLong(split[4])
                        );

                        //2.只要pv数据
                        if ("pv".equals(userBehavior.getBehavior())) {
                            out.collect(userBehavior);
                        }
                    }
                });


        SingleOutputStreamOperator<Integer> process = userBehaviorSingleOutputStreamOperator.keyBy(data -> "UV")
                .process(new KeyedProcessFunction<String, UserBehavior, Integer>() {
                    private int count = 0;
                    private HashSet<Long> uids = new HashSet<>();

                    @Override
                    public void processElement(UserBehavior value, Context ctx, Collector<Integer> out) throws Exception {

                        if (!uids.contains(value.getUserId())) {
                            uids.add(value.getUserId());
                            count++;
                            out.collect(count);
                        }
                    }
                });

        process.print();


        env.execute();
    }
}
